package com.qf.mao.service;

import com.qf.mao.dao.CourseDao;
import com.qf.mao.entity.Course;
import com.qf.mao.utils.CommonsUtils;

import java.sql.SQLException;
import java.util.List;

/**
 * @author 啾咪小魔王
 * @date 2022/4/7 9:51
 * @since JDK 1.8
 */
@SuppressWarnings("all")
public class CourseService {
    CourseDao courseDao = new CourseDao();

    public List<Course> getCourse() {
        try {
            return courseDao.getCourse();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public Integer addCourse(String courseName) {
        if (courseName == null || "".equals(courseName)) {
            return CommonsUtils.ERROR_ELSE;
        }
        try {
            //根据课程名查询课程
            Course name = courseDao.getCourseByCourseName(courseName);
            if (name != null) {
                //课程名重复，添加失败
                return CommonsUtils.ERROR_NAME;
            }
            Integer integer = courseDao.addCourse(courseName);
            return integer == 1 ? CommonsUtils.SUCCESS : CommonsUtils.ERROR_ELSE;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return CommonsUtils.ERROR_ELSE;
    }
    public Integer deleteCourseByCid(String cid) {
        Integer r = null;
        try {
            r = courseDao.deleteCourseByCid(Integer.parseInt(cid));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return r == 1 ? CommonsUtils.SUCCESS_DELETE : CommonsUtils.ERROR_ELSE;
    }
}
